package com.kai.controller;

import com.kai.pojo.User;
import com.kai.service.UserService;
import com.kai.utils.MD5Utils;
import com.kai.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Controller
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 查询全部用户或模糊查寝
     * @return
     */
    @RequestMapping("/selectAllUser")
    @ResponseBody
    public Map<String, Object> selectAllUser(String name, String sex, int pageNum, int pageSize){
        HashMap<String, Object> map = new HashMap<>();
        int pageTotal = userService.selectAllUserCount(name,sex);
        List<User> userList = userService.selectAllUser(name,sex,(pageNum-1)*pageSize,pageSize);
        map.put("userList",userList);
        map.put("pageTotal",pageTotal);
        return map;
    }
    /**
     * 修改用户状态
     * @param state
     * @param id
     * @return
     */
    @RequestMapping("/updateUserState")
    @ResponseBody
    public String updateUserState(String state,String id){
        return userService.updateUserState(state, id)+"";
    }

    /**
     * 修改用户信息或添加用户
     * @param user
     * @return
     */
    @RequestMapping("/updateUserInfo")
    @ResponseBody
    public String updateUserInfo(@RequestBody User user){
        if (!user.getId().equals("")){
            if (!StringUtils.isEmpty(user.getPassword())){
                String userPassword = user.getPassword();
                user.setPassword(MD5Utils.MD5(userPassword));
            }
            userService.updateUserInfo(user);
            return "updateUser";
        }
        else {
            if (userService.selectUserByName(user.getName())>0){
                return "exist";
            }
            user.setId(UUIDUtils.getUUID());
            user.setPassword(MD5Utils.MD5(user.getPassword()));
            user.setCreatetime(new Date());
            userService.addUser(user);
            return "addUser";
        }
    }

    /**
     *删除用户
     * @param ids
     * @return
     */
    @RequestMapping("/deleteUser")
    @ResponseBody
    public int deleteUser(String[] ids){
        int i = 0;
        List<String> idList = Stream.of(ids).distinct().collect(Collectors.toList());
        for (String id : idList) {
            i = userService.deleteUser(id);
            i++;
        }
        return i;
    }
    /*修改密码*/
    @PostMapping("/updatePassword")
    @ResponseBody
    public int updatePassword(String id,String password){
        return userService.updatePassword(id,MD5Utils.MD5(password));
    }
    /*判断旧密码是不正确*/
    @GetMapping("/oldPassword")
    @ResponseBody
    public int oldPassword(@RequestParam("password") String password,@RequestParam("id") String id){
        return userService.oldPassword(MD5Utils.MD5(password),id);
    }

    /*查看个人信息*/
    @GetMapping("/userInfo")
    @ResponseBody
    public User userInfo(HttpSession session){
        User loginUser = (User) session.getAttribute("loginUser");
        return loginUser;
    }
}
